package com.sservice.web.controller.admin;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.sservice.web.utils.CreateImageCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 图片验证码生成服务
 * <p>
 * Created by liushaokang on 2017/3/14.
 */

@Controller
@RequestMapping("admin/code")
public class CaptchaController {
//    private Logger logger = LoggerFactory.getLogger(CaptchaController.class);

//    @Autowired
//    private Producer captchaProducer = null;

    @RequestMapping(value = "captcha-image")
    public void getCustomImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);


        CreateImageCode vCode = new CreateImageCode(100, 30, 5, 10);
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, vCode.getCode());
        vCode.write(response.getOutputStream());
    }
}
